<template>
  <a-card :bordered="false">
    <div class="table-operator">
      <a-button v-if="hasPerm('PersonnelAllotment.assignment')" type="primary" @click="PllAoo()">客户组分配</a-button>
      <a-button type="primary" icon="redo" @click="getDataList()">刷新</a-button>
      <a-radio-group
        v-model="queryParam[2].Keyword"
        style="margin-left: 8px"
        :default-value="null"
        button-style="solid"
        @change="getDataList()"
      >
        <a-radio-button :value="null"> 全部</a-radio-button>
        <a-radio-button :value="0"> 待分组</a-radio-button>
        <a-radio-button :value="1"> 申请中</a-radio-button>
        <a-radio-button :value="2"> 已分组</a-radio-button>
      </a-radio-group>

      <!--<a-radio-group-->
      <!--  v-model="queryParam[3].Keyword"-->
      <!--  style="margin-left: 8px"-->
      <!--  :default-value="null"-->
      <!--  button-style="solid"-->
      <!--  @change="getDataList()"-->
      <!--&gt;-->
      <!--  <a-radio-button :value="null"> 全部</a-radio-button>-->
      <!--  <a-radio-button :value="1"> 已分配</a-radio-button>-->
      <!--  <a-radio-button :value="0"> 待分配</a-radio-button>-->
      <!--</a-radio-group>-->
    </div>

    <div class="table-page-search-wrapper">
      <a-form layout="inline">
        <a-row :gutter="10">
          <a-col :md="4" :sm="24">
            <a-form-item>
              <!-- <a-input @keydown="inputKeyDown" v-model="queryParam[0].Keyword" placeholder="客户名称" /> -->
              <searchTip
                v-model="queryParam[0].Keyword"
                style=""
                placeholder="客户名称"
                :list="searchTipResultList"
                @enterKeyDown="getDataList"
                @change="toGetSearchTipList"
                @blur="keywordBlurFun"
                @select="selectSearchTipFun"
              />
            </a-form-item>
          </a-col>
          <a-col :md="5" :sm="24">
            <a-form-item>
              <a-select v-model="queryParam[1].Keyword" style="width: 250px" placeholder="选择分组">
                <a-select-option v-for="item in List" :key="item.Id" :value="item.GroupName">
                  {{ item.GroupName }}
                </a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="24">
            <a-button
              type="primary"
              @click="
                () => {
                  this.pagination.current = 1
                  this.getDataList()
                }
              "
              >查询
            </a-button>
            <a-button style="margin-left: 8px" @click="Repossess">重置</a-button>
          </a-col>
        </a-row>
      </a-form>
    </div>

    <a-table
      ref="table"
      :scroll="{ y: 'calc(100vh - 370px)' }"
      :columns="columns"
      :rowKey="(row, index) => index"
      :dataSource="data"
      :pagination="pagination"
      :loading="loading"
      @change="handleTableChange"
      :bordered="true"
      size="small"
    >
      <!--<template slot="AllocationStatus" slot-scope="text, record">-->
      <!--  <a-tag v-if="record.AllocationStatus !== 1" color="red">待分配</a-tag>-->
      <!--  <a-tag v-else-if="record.AllocationStatus === 1" color="green">已分配</a-tag>-->
      <!--</template>-->
      <template slot="GroupStatus" slot-scope="text, record">
        <a-tag v-if="record.GroupStatus === 0" color="red">待分组</a-tag>
        <a-tag v-if="record.GroupStatus === 1" color="orange">申请中</a-tag>
        <a-tag v-if="record.GroupStatus === 2" color="green">已分组</a-tag>
      </template>
      <span slot="CreateTime" slot-scope="CreateTime">{{ timeFormat(CreateTime) }}</span>
      <span slot="action" slot-scope="text, record">
        <template>
          <a v-if="hasPerm('Cust_CustomerInfoFormal.Edit')" @click="handleRevise(record.cCusCode)">编辑</a>
          <a-divider
            v-if="hasPerm('Cust_CustomerInfoFormal.Edit') && hasPerm('Cust_CustomerInfoFormal.Delete')"
            type="vertical"
          />
          <a @click="contactPerson(record)">联系人</a>
        </template>
      </span>
    </a-table>

    <PllAOO :parentObj="this" ref="PllAOO" />

    <!-- <List ref="List" /> -->
  </a-card>
</template>

<script>
// 客户分配

import PllAOO from '../Customer/PllAOO.vue'

import moment from 'moment'

const columns = [
  { title: '客户编码', dataIndex: 'cCusCode', width: '90px' },
  { title: '客户名称', dataIndex: 'cCusName', align: 'left', width: '220px' },
  {
    title: '分配给',
    dataIndex: 'AllocationsList',
    customRender: (value, row) => {
      if (row.AllocationsList.length > 0) {
        return row.AllocationsList.map((item) => item.SalesmanName).join('/')
      } else {
        return ''
      }
    },
    width: '200px',
  },
  {
    title: '所属分组',
    dataIndex: 'GroupName',
    align: 'center',

    width: '90px',
  },
  {
    title: '分组状态',
    dataIndex: 'GroupStatus',
    align: 'center',
    scopedSlots: { customRender: 'GroupStatus' },
    width: '90px',
  },
  // {
  //   title: '分配状态',
  //   dataIndex: 'AllocationStatus',
  //   align: 'center',
  //   scopedSlots: { customRender: 'AllocationStatus' },
  //   width: '90px'
  // }
  // {
  //   title: '销售经理',
  //   dataIndex: 'AllocationName',
  //   width: '130px',
  //   customRender: (value, row) => {
  //     if (row.DistributionUserList.length > 0) {
  //       return row.DistributionUserList.filter(item => item.Type == 1)
  //         .map(item => item.UserName)
  //         .join(',')
  //     } else {
  //       return ''
  //     }
  //   }
  // },
  // {
  //   title: '市场经理',
  //   dataIndex: 'shichang',
  //   width: '100px',
  //   customRender: (value, row) => {
  //     if (row.DistributionUserList.length > 0) {
  //       return row.DistributionUserList.filter(item => item.Type == 2)
  //         .map(item => item.UserName)
  //         .join(',')
  //     } else {
  //       return ''
  //     }
  //   }
  // }
  // { title: '操作', dataIndex: 'action', align: 'left', scopedSlots: { customRender: 'action' }, width: '15%' }
]
import searchTip from '@/components/components/SearchTip.vue'

export default {
  components: {
    PllAOO,
    searchTip,
  },
  mounted() {
    this.getDataList()

    this.selectedRowKeys = []

    this.loading = true
    this.$http.post('/CustomerManagement/CustomerGroup/GetDataListByMoreCondition', {}).then((resJson) => {
      this.List = resJson.Data

      if (this.List) {
        const pagination = { ...this.pagination }
        pagination.total = resJson.Total
        this.pagination = pagination
      }
    })
  },
  data() {
    return {
      IsFormal: true,
      data: [],
      List: [], //分组
      listChildren: [], //市
      county: [], //县
      pagination: {
        current: 1,
        pageSize: 25,
        showTotal: (total, range) => `总数:${total} 当前:${range[0]}-${range[1]}`,
        pageSizeOptions: ['25', '50', '100'], // 设置pageSize的可选值，页面可以通过下拉框进行选择
        showSizeChanger: true,
      },
      filters: {},
      sorter: { field: 'cCusCode', order: 'desc' },

      queryParam: [
        { Condition: 'cCusName' },
        { Condition: 'GroupName' },
        { Condition: 'GroupStatus', Keyword: null },
        { Condition: 'AllocationStatus', Keyword: null },
      ],
      loading: false,
      columns,
      selectedRowKeys: [],
      selectedRows: [],
      searchTipResultList: [],
    }
  },
  methods: {
    inputKeyDown(e) {
      if (e.keyCode === 13) {
        if (!this.queryParam[0].Keyword) return this.$message.error('请输入查询条件')

        this.getDataList()
      }
    },
    PllAoo() {
      this.$refs.PllAOO.openForm()
    },
    handleAdd() {
      this.$refs.Detail.openForm(null, '添加')
    },
    handleRevise(id) {
      this.$refs.Detail.openForm(id, '编辑')
    },
    // 省市区的模糊查询
    filterOption(input, option) {
      return option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0
    },

    Repossess() {
      this.queryParam = [
        { Condition: 'cCusName' },
        { Condition: 'GroupName' },
        { Condition: 'GroupStatus', Keyword: null },
        { Condition: 'AllocationStatus', Keyword: null },
      ]
      this.getDataList()
    },
    CheckboxProps(record) {
      return {
        props: {
          disabled: record.AllocationStatus === 1,
        },
      }
    },
    handleTableChange(pagination, filters, sorter) {
      this.pagination = { ...pagination }
      this.filters = { ...filters }

      this.getDataList()
    },
    Distribute(row) {
      this.IsFormal = true
      this.$refs.MyCustomer.openForm(row, '分配客户')
    },
    getDataList() {
      this.searchTipResultList = [] //清空搜索提示列表
      this.selectedRowKeys = []

      this.loading = true
      this.$http
        .post('/CustomerManagement/CustomerInfo/GetDataListByMoreConditionOfFormal', {
          PageIndex: this.pagination.current,
          PageRows: this.pagination.pageSize,
          SortField: this.sorter.field || 'Id',
          SortType: this.sorter.order,
          DeptId: this.DeptId,
          Search: this.queryParam,
          ...this.filters,
        })
        .then((resJson) => {
          this.loading = false
          this.data = resJson.Data
          if (this.data) {
            const pagination = { ...this.pagination }
            pagination.total = resJson.Total
            this.pagination = pagination
          }
        })
    },
    onSelectChange(selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
    },
    hasSelected() {
      return this.selectedRowKeys.length > 0
    },
    contactPerson(row) {
      this.$refs.List.openForm(row, '客户联系人')
    },

    handleEdit(id) {
      this.$refs.details.openForm(id, '客户详情')
    },
    handleDelete(ids) {
      var thisObj = this
      this.$confirm('确认删除吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(() => {
        return new Promise((resolve, reject) => {
          thisObj.$http.post('/CustomerManagement/Cust_CustomerInfoFormal/DeleteData', ids).then((resJson) => {
            resolve()

            if (resJson.Success) {
              thisObj.$message.success('操作成功!')

              thisObj.getDataList()
            } else {
              thisObj.$message.error(resJson.Msg)
            }
          })
        })
      })
    },
    timeFormat(val) {
      // 时间格式化函数
      return moment(val).format('YYYY-MM-DD HH:mm')
    },
    async toGetSearchTipList(e) {
      this.searchTipResultList = []
      this.queryParam[0].Keyword = e
      if (!this.queryParam[0].Keyword) {
        return
      }
      await this.$http
        .post('/CustomerManagement/CustomerInfo/GetDataListByMoreConditionOfFormal', {
          PageIndex: 1,
          PageRows: 10,
          SortField: this.sorter.field || 'Id',
          SortType: this.sorter.order,
          DeptId: this.DeptId,
          Search: this.queryParam,
          ...this.filters,
        })
        .then((resJson) => {
          if (resJson.Success && this.queryParam[0].Keyword) {
            this.searchTipResultList = resJson.Data.map((e) => e.cCusAbbName).slice(0, 10)
          } else {
            this.searchTipResultList = []
          }
        })
        .finally(() => {
          this.loading = false
        })
    },

    selectSearchTipFun(text) {
      this.queryParam[0].Keyword = text
      this.searchTipResultList = []
      this.getDataList()
    },

    keywordBlurFun() {
      this.searchTipResultList = []
    },
  },
}
</script>
<style lang="less" scoped>
/deep/ .ant-card-body {
  padding-bottom: 0px;
}
</style>
